package com.qfedu.dao.impl;

import com.qfedu.dao.AddressDao;
import com.qfedu.entity.Address;
import com.qfedu.utils.BaseDao;

import java.sql.SQLException;
import java.util.List;

public class AddressDaoImpl extends BaseDao implements AddressDao {
    @Override
    public List<Address> findAddressById(int uid) throws SQLException {
        String sql = "select a_id as aid, u_id as uid,a_name as aname," +
                "a_phone as aphone,a_detail as adetail ,a_state as astate " +
                "from address where u_id = ? order by a_state desc";
        List<Address> addressList = queryBeanList(sql, Address.class, uid);
        return addressList;
    }

//    @Override
//    public List<Address> findAddress() throws SQLException {
//        String sql = "select a_id as aid, u_id as uid,a_name as aname," +
//                "a_phone as aphone,a_detail as adetail ,a_state as astate " +
//                "from address";
//        List<Address> addressList = queryBeanList(sql, Address.class, null);
//        return addressList;
//    }

    @Override
    public int addAddress(Address address) throws SQLException {
        String sql = "insert into address (u_id,a_name,a_phone,a_detail,a_state) values(?,?,?,?,?)";
        int num = update(sql,address.getUid(), address.getAname(), address.getAphone(), address.getAdetail(), address.getAstate());
        return num;
    }

    @Override
    public int updateAddress(Address address) throws SQLException {
        String sql = "update address set a_name = ?,a_phone=?,a_detail=? where a_id = ?";
        int num = update(sql, address.getAname(), address.getAphone(), address.getAdetail(), address.getAid());
        return num;
    }

    @Override
    public int deleteAddress(int aid) throws SQLException {
        String sql = "delete from address where a_id = ?";
        int num = update(sql,aid);
        return num;
    }

    @Override
    public int defaultAddr(int aid) throws SQLException {
        String sql = "update address set a_state = 1 where a_id = ?";
        int num = update(sql, aid);
        return num;
    }

    @Override
    public int emptyDefault(int uid) throws SQLException {
        String sql = "update address set a_state = 0 where u_id = ?";
        int update = update(sql, uid);
        return update;
    }
}
